<template>
  <div>
    <common-head />
     <div class="wrap">
     <van-form @submit="onSubmit">
      <van-field
        v-model="userInfo.username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' }
        ]"
      />
      <van-field
        v-model="userInfo.pwd"
        type="password"
        name="pwd"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>

    <van-button @click="$router.push('/login')">去登录</van-button>
      </div>
   </div>
</template>

<script>
import CommonHead from 'components/CommonHead'
import { register } from 'api'
import { Toast } from 'vant'
export default {
  data () {
    return {
      userInfo: {
        username: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (params) {
      // 注册
      register(params).then(res => {
        if (res.data.code === 0) {
          // 注册成功
          Toast.success({
            message: '注册成功',
            duration: 1000,
            onClose: () => {
              // 去登录页
              this.$router.replace('/login')
            }
          })
        } else {
          Toast.fail('注册失败')
        }
      })
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>

</style>
